package com.tzjz.utils;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import  cn.hutool.crypto.symmetric.SM4;
import cn.hutool.crypto.SmUtil;

public class CryptoUtil {
    /*
     当key为空时，随机加密
     key不为空时，默认密钥长度为128
     */
    public static  String encryptHex(String content,String secretKey) {
        String encryptStr = "";
        if (StrUtil.isEmpty(content))
            return encryptStr;
        SM4 sm4 = null;
        if (StrUtil.isEmpty(secretKey))
            sm4 = SmUtil.sm4();
        else
            sm4 = new SM4(Mode.ECB, Padding.PKCS5Padding, secretKey.getBytes());
        encryptStr = sm4.encryptHex(content);
        return encryptStr;
    }
    /*
     * 解密
     * */
    public  static  String decryptStr(String content,String secretKey)
    {
        String decrytStr="";
        if(StrUtil.isEmpty(content))
            return  decrytStr;
        SM4 sm4 = null;
        if (StrUtil.isEmpty(secretKey))
            sm4 = SmUtil.sm4();
        else
            sm4 = new SM4(Mode.ECB, Padding.PKCS5Padding, secretKey.getBytes());
        decrytStr=sm4.decryptStr(content, CharsetUtil.CHARSET_UTF_8);
        return  decrytStr;
    }

}
